import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jc.util.HttpPostUtil;
import com.jc.util.R;
import com.jc.vo.ApiConfig;
import com.jc.vo.PolicyVO;
import com.jc.vo.QuotaApplyVO;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;

public class calculatePolicyDemo {


    // jc提供的SecretId
    public static String SECRET_ID = "100001";
    // jc提供的SecretKey
    public static String SECRET_KEY = "a28aa7399a067919c913ead1b434552c";


    public static String SERVICE_HOST = "127.0.0.1:8080";

//    public static String SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/cross/v1/calculatePolicy";
    public static String SERVICE_URL = "http://" + SERVICE_HOST + "/jc-order/traditional/v1/calculatePolicy";


    @Test
    public void test() throws ParseException {
        PolicyVO vo =getVo("1");
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, SERVICE_URL);
        try {

            ObjectMapper om = new ObjectMapper();
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            String response = HttpPostUtil.send(om.writeValueAsString(vo), config, false);
            R r = om.readValue(response, R.class);
            try {
                if (r.getSuccess()) {
                    QuotaApplyVO result = om.convertValue(r.getContent(), QuotaApplyVO.class);
                    r.setContent(result);
                }
            } catch (Exception e) {
                r.setSuccess(false);
                r.setCode(R.RCODE.SYS_ERROR.getCode());
                r.setMessage(R.RCODE.SYS_ERROR.getMessage());
                e.printStackTrace();
            }
            System.out.println(r.getMessage());
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private PolicyVO getVo(String productCode){
        PolicyVO vo =new PolicyVO();
        vo.setProductCode(productCode);
        //保额确定方式  货值x加成比例	1 ; 货值加运费	2
        vo.setInsuranceConfirmType(1);
//        vo.setInsuranceConfirmType(1);
        vo.setGoodsValueAmount(new BigDecimal("1000"));
        vo.setCurrency("USD");

        if (2 == vo.getInsuranceConfirmType()) {
            vo.setDeliveryCurrency("USD");
            vo.setDeliveryAmount(new BigDecimal("500"));
        }
        if (1 == vo.getInsuranceConfirmType()) {
            vo.setAppendPercent(new BigDecimal("110"));
        }


        if("2".equalsIgnoreCase(productCode)) {
            vo.setTransportType(2);
            vo.setDispatchType(2);
            vo.setShelfGuarantee(3);
            vo.setWeightPackaged(new BigDecimal("20"));
        }else{
            vo.setWarehouseTermsRequired(true);
        }

        return vo;

    }
}
